﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.ModFlow.Domain.DomainLayer;
using HAUtilWpf;
using HA.ModFlow.Controls.UserControls;


namespace HA.ModFlow.Controls.Windows
{
    /// <summary>
    /// winEVT.xaml 的交互逻辑
    /// </summary>
    public partial class winEVT : Window
    {
        public winEVT(ModFlowData mfData)
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window_Loaded);
            this.cbOption.SelectionChanged += new SelectionChangedEventHandler(cbOption_SelectionChanged);
            this.cbIndex.SelectionChanged += new SelectionChangedEventHandler(cbIndex_SelectionChanged);
            this.cbView.SelectionChanged += new SelectionChangedEventHandler(cbView_SelectionChanged);
            this.cbPre.Checked+=new RoutedEventHandler(cbPre_Checked);
            this.btnReset.Click += new RoutedEventHandler(btnReset_Click);
            this.btnOk.Click += new RoutedEventHandler(btnOk_Click);
            this.cbElevation.Content = "蒸散界面海拔(" + Domain.DomainLayer.UnitConverter.GetLength(mfData.ModelOption.Units.Length) + ")";
            this.cbRate.Content = "最大蒸散速率(" + Domain.DomainLayer.UnitConverter.GetLength(mfData.ModelOption.Units.Length) + "/" + Domain.DomainLayer.UnitConverter.GetTime(mfData.ModelOption.Units.Time) + ")";
            this.cbDepth.Content = "蒸散终止深度(" + Domain.DomainLayer.UnitConverter.GetLength(mfData.ModelOption.Units.Length) + ")";
            this._evts = new PCEVT(mfData);

            

            //显示第一个应力期的时间点
            this.labStart.Content = "0.0";
            if (_evts.TimePoint.Length <= 1)
                return;
            else
                this.labFinish.Content = _evts.TimePoint[1].ToString();

            this.cbIndex.Items.Clear();
            this.cbIndex.ItemsSource = this._evts.PeriodIndex;

            //当Use previous 选中时不可修改各属性值
            Binding bindEabled = new Binding();
            bindEabled.ElementName = "cbPre";
            bindEabled.Path = new PropertyPath("IsChecked");
            bindEabled.Converter = new NotConvertor();
            this.btnReset.SetBinding(Button.IsEnabledProperty, bindEabled);

            Binding bindEable = new Binding();
            bindEable.ElementName = "cbPre";
            bindEable.Path = new PropertyPath("IsChecked");
            bindEable.Converter = new BoolToVisityConverter();
            myArray.dgData.SetBinding(DataGrid.VisibilityProperty, bindEable);

            Binding bindOption = new Binding();
            bindOption.Source = Evts.Evt;
            bindOption.Path = new PropertyPath("Option");
            bindOption.Converter = new EVTOptionToIntConverter();
            cbOption.SetBinding(ComboBox.SelectedIndexProperty, bindOption);
            //初始选择 Max ET rate，double
            this.myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._evts.MaxETRateData[cbIndex.SelectedIndex]);
            this.myArray.SetDoubleData(this._evts.MaxETRateData[cbIndex.SelectedIndex]);
        }

        #region 字段及属性



        PCEVT _evts;

        public PCEVT Evts
        {
            get { return _evts; }
            set { _evts = value; }
        }



        #endregion

        #region 事件

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this.Evts;
        }

        private void cbOption_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cbOption.SelectedIndex == 0 && this.cbView.SelectedIndex == 3)
            {
                this.cbView.SelectedIndex = 1;
            }
        }

        private void cbIndex_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cbIndex.SelectedIndex != -1)
            {
                int index = this.cbIndex.SelectedIndex;

                string start = _evts.TimePoint[index].ToString();
                string finish = _evts.TimePoint[index + 1].ToString();

                if (start.Contains("."))
                {
                    this.labStart.Content = start;
                }
                else
                {
                    this.labStart.Content = start + ".0";
                }

                if (finish.Contains("."))
                {
                    this.labFinish.Content = finish;
                }
                else
                {
                    this.labFinish.Content = finish + ".0";
                }

                Binding bindMut = new Binding();
                bindMut.Source = _evts.Evt.Periods[index];
                bindMut.Path = new PropertyPath("Multiplier");
                bindMut.Converter = new NumberToStringConverter();
                this.txtMultiplier.SetBinding(TextBox.TextProperty, bindMut);

                Binding bindUsepre = new Binding();
                bindUsepre.Source = _evts;
                bindUsepre.Path = new PropertyPath("IsUsePre[" + index + "]");
                this.cbPre.SetBinding(CheckBox.IsCheckedProperty, bindUsepre);

                switch (cbView.SelectedIndex)
                {
                    case 0: myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._evts.ElevationData[index]);
                            this.myArray.SetDoubleData(this._evts.ElevationData[cbIndex.SelectedIndex]);
                        break;
                    case 1: myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._evts.MaxETRateData[index]);
                            this.myArray.SetDoubleData(this._evts.MaxETRateData[cbIndex.SelectedIndex]);
                        break;
                    case 2: myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._evts.ExtinctDepthData[index]);
                            this.myArray.SetDoubleData(this._evts.ExtinctDepthData[cbIndex.SelectedIndex]);
                        break;
                    case 3: myArray.dgData.ItemsSource = PCGridArrayLayer<int>.GetBindable2DArray<int>(this._evts.EvtLayerData[index]);
                            this.myArray.SetIntData(this._evts.EvtLayerData[cbIndex.SelectedIndex]);
                        break;
                }
            }
        }

        private void cbView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = this.cbIndex.SelectedIndex;
            switch (cbView.SelectedIndex)
            {
                case 0: myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._evts.ElevationData[index]);
                        this.myArray.SetDoubleData(this._evts.ElevationData[cbIndex.SelectedIndex]);
                    break;
                case 1: myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._evts.MaxETRateData[index]);
                        this.myArray.SetDoubleData(this._evts.MaxETRateData[cbIndex.SelectedIndex]);
                    break;
                case 2: myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._evts.ExtinctDepthData[index]);
                        this.myArray.SetDoubleData(this._evts.ExtinctDepthData[cbIndex.SelectedIndex]);
                    break;
                case 3: myArray.dgData.ItemsSource = PCGridArrayLayer<int>.GetBindable2DArray<int>(this._evts.EvtLayerData[index]);
                        this.myArray.SetIntData(this._evts.EvtLayerData[cbIndex.SelectedIndex]);
                    break;
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            if (cbView.SelectedIndex == 3)
            {
                winResetValue winReset = new winResetValue(DataType.INT);
                winReset.Owner = this;
                winReset.ShowDialog();

                if (winReset.DialogResult == true)
                {
                    double xxx = Convert.ToDouble(winReset.txtResetValue.Text);
                    ResetIntData(Convert.ToInt32(xxx));
                }
            }
            else
            {
                winResetValue winReset = new winResetValue(DataType.DOUBLE);
                winReset.Owner = this;
                winReset.ShowDialog();

                if (winReset.DialogResult == true)
                {
                    ResetDoubleData(Convert.ToDouble(winReset.txtResetValue.Text));
                }
            }
                    
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.Evts.ConfirmToDataCenter();
            this.DialogResult = true;
            this.Close();
        }

        private void cbPre_Checked(object sender, RoutedEventArgs e)
        {
            int index = this.cbIndex.SelectedIndex;
            if (index > 0)
            {
                this._evts.ElevationData[index] = (double[,])this._evts.ElevationData[index ].Clone();
                this._evts.ExtinctDepthData[index] = (double[,])this._evts.ExtinctDepthData[index ].Clone();
                this._evts.MaxETRateData[index] = (double[,])this._evts.MaxETRateData[index].Clone();

                if (this.cbOption.SelectedIndex == 1)
                {
                    this._evts.EvtLayerData[index] = (int[,])this._evts.EvtLayerData[index - 1].Clone();
                }
            }
        }

        #endregion

        #region 辅助方法
        //重置层为单一值

        public void ResetIntData(int data)
        {
            int index = this.cbIndex.SelectedIndex;

            for (int i = 0; i < this._evts.EvtLayerData[index].GetLength(0); i++)
            {
                for (int j = 0; j < this._evts.EvtLayerData[index].GetLength(1); j++)
                {
                    this._evts.EvtLayerData[index][i, j] = data;
                }
            }
            int k = index + 1;

            for (int p = k; p < this.Evts.IsUsePre.Count; p++)
            {               
                    if (this.Evts.IsUsePre[p])
                    {
                        for (int i = 0; i < this._evts.EvtLayerData[p].GetLength(0); i++)
                        {
                            for (int j = 0; j < this._evts.EvtLayerData[p].GetLength(1); j++)
                            {
                                this._evts.EvtLayerData[p][i, j] = data;
                            }
                        }
                    }
            }
           

            this.myArray.dgData.Items.Refresh();
        }

        public void ResetDoubleData(double data)
        {
            int index = this.cbIndex.SelectedIndex;
            if (this.cbView.SelectedIndex == 1)
            {
                for (int i = 0; i < this._evts.MaxETRateData[index].GetLength(0); i++)
                    for (int j = 0; j < this._evts.MaxETRateData[index].GetLength(1); j++)
                        this._evts.MaxETRateData[index][i, j] = data;
            }
            else if (this.cbView.SelectedIndex == 2)
            {
                for (int i = 0; i < this._evts.ExtinctDepthData[index].GetLength(0); i++)
                    for (int j = 0; j < this._evts.ExtinctDepthData[index].GetLength(1); j++)
                        this._evts.ExtinctDepthData[index][i, j] = data;
            }
            else
            {
                for (int i = 0; i < this._evts.ElevationData[index].GetLength(0); i++)
                    for (int j = 0; j < this._evts.ElevationData[index].GetLength(1); j++)
                        this._evts.ElevationData[index][i, j] = data;
            }

            int k = index + 1;

            while ( k < this.Evts.IsUsePre.Count &&this.Evts.IsUsePre[k])
            {
                if (this.cbView.SelectedIndex == 1)
                {
                    for (int i = 0; i < this._evts.MaxETRateData[k].GetLength(0); i++)
                        for (int j = 0; j < this._evts.MaxETRateData[k].GetLength(1); j++)
                            this._evts.MaxETRateData[k][i, j] = data;
                }
                else if (this.cbView.SelectedIndex == 2)
                {
                    for (int i = 0; i < this._evts.ExtinctDepthData[k].GetLength(0); i++)
                        for (int j = 0; j < this._evts.ExtinctDepthData[k].GetLength(1); j++)
                            this._evts.ExtinctDepthData[k][i, j] = data;
                }
                else
                {
                    for (int i = 0; i < this._evts.ElevationData[k].GetLength(0); i++)
                        for (int j = 0; j < this._evts.ElevationData[k].GetLength(1); j++)
                            this._evts.ElevationData[k][i, j] = data;
                }

                k++;
            }
            this.myArray.dgData.Items.Refresh();
        }

        #endregion


    }

    public class EVTOptionToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            switch ((EVTOPTION)value)
            {
                case EVTOPTION.TOPLAYER: return 0;
                case EVTOPTION.VERTICALCELLS: return 1;
                default: throw new NotImplementedException();
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((int)value)
            {
                case 0: return EVTOPTION.TOPLAYER;
                case 1: return EVTOPTION.VERTICALCELLS;
                default: throw new NotImplementedException();
            }
        }
    }
}
